<template>
  <el-dialog
    v-model="visible"
    width="900px"
    :close-on-click-modal="false"
    append-to-body
    class="unified-dialog detail-dialog-grouped"
  >
    <template #header>
      <DialogHeader
        icon="Document"
        title="分组详情示例"
        description="使用 detail-section 分组展示，适用于需要按功能模块分组的场景"
        type="primary"
      />
    </template>
    
    <div v-if="data" class="dialog-content">
      <!-- 基础信息分组 -->
      <div class="detail-section">
        <div class="section-title">
          <el-icon><Document /></el-icon>
          <span>基础信息</span>
        </div>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="ID">{{ data.id }}</el-descriptions-item>
          <el-descriptions-item label="配置ID">{{ data.config_id }}</el-descriptions-item>
          <el-descriptions-item label="Topic名称">
            <el-tag type="primary" size="small">{{ data.topic_name }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="分区ID">{{ data.partition_id }}</el-descriptions-item>
          <el-descriptions-item label="最新Offset">{{ data.latest_offset }}</el-descriptions-item>
          <el-descriptions-item label="消费滞后">
            <el-tag :type="data.consumer_lag > 1000 ? 'danger' : 'warning'" size="small">
              {{ data.consumer_lag }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="Topic大小">{{ formatBytes(data.topic_size_bytes) }}</el-descriptions-item>
          <el-descriptions-item label="消息Key">{{ data.message_key || '-' }}</el-descriptions-item>
        </el-descriptions>
      </div>
      
      <!-- 监控配置分组 -->
      <div class="detail-section">
        <div class="section-title">
          <el-icon><View /></el-icon>
          <span>监控配置</span>
        </div>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="监控状态">
            <el-tag :type="data.is_monitored ? 'success' : 'info'" size="small">
              {{ data.is_monitored ? '已启用' : '已禁用' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="告警启用">
            <el-tag :type="data.is_alert_enabled ? 'success' : 'info'" size="small">
              {{ data.is_alert_enabled ? '已启用' : '已禁用' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="告警阈值">{{ data.alert_threshold_hours }} 小时</el-descriptions-item>
          <el-descriptions-item label="最大告警次数">{{ data.max_alert_count }}</el-descriptions-item>
          <el-descriptions-item label="首次告警时间">{{ data.alert_first_send_time }}</el-descriptions-item>
          <el-descriptions-item label="第2次告警间隔">{{ data.alert_second_interval }} 秒</el-descriptions-item>
          <el-descriptions-item label="第3次告警间隔">{{ data.alert_third_interval }} 秒</el-descriptions-item>
          <el-descriptions-item label="Webhook URL" :span="2">
            <span class="text-muted">{{ data.webhook_url || '-' }}</span>
          </el-descriptions-item>
        </el-descriptions>
      </div>
      
      <!-- 状态信息分组 -->
      <div class="detail-section">
        <div class="section-title">
          <el-icon><InfoFilled /></el-icon>
          <span>状态信息</span>
        </div>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="状态">
            <el-tag :type="getStatusType(data.status)" size="small">
              {{ getStatusText(data.status) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="告警状态">
            <el-tag :type="getAlertStatusType(data.alert_status)" size="small">
              {{ getAlertStatusText(data.alert_status) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="告警次数">{{ data.alert_count }}</el-descriptions-item>
          <el-descriptions-item label="告警历史ID">{{ data.alert_history_id || '-' }}</el-descriptions-item>
          <el-descriptions-item label="首次告警时间">{{ data.first_alert_time || '-' }}</el-descriptions-item>
          <el-descriptions-item label="最后告警时间">{{ data.last_alert_time || '-' }}</el-descriptions-item>
        </el-descriptions>
      </div>
      
      <!-- 时间信息分组 -->
      <div class="detail-section">
        <div class="section-title">
          <el-icon><Clock /></el-icon>
          <span>时间信息</span>
        </div>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="最新消息时间">{{ data.message_timestamp || '-' }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ data.created_at || '-' }}</el-descriptions-item>
          <el-descriptions-item label="更新时间">{{ data.updated_at || '-' }}</el-descriptions-item>
        </el-descriptions>
      </div>
    </div>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { computed } from 'vue'
import { Document, View, InfoFilled, Clock } from '@element-plus/icons-vue'
import DialogHeader from '@/components/DialogHeader.vue'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['update:modelValue'])

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const handleClose = () => {
  visible.value = false
}

const formatBytes = (bytes) => {
  if (!bytes || bytes === 0) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return (bytes / Math.pow(k, i)).toFixed(2) + ' ' + sizes[i]
}

const getStatusType = (status) => {
  const typeMap = {
    'active': 'success',
    'inactive': 'info',
    'monitored_only': 'warning'
  }
  return typeMap[status] || 'info'
}

const getStatusText = (status) => {
  const textMap = {
    'active': '活跃',
    'inactive': '非活跃',
    'monitored_only': '仅监控'
  }
  return textMap[status] || status
}

const getAlertStatusType = (status) => {
  const typeMap = {
    'normal': 'success',
    'alerting': 'danger'
  }
  return typeMap[status] || 'info'
}

const getAlertStatusText = (status) => {
  const textMap = {
    'normal': '正常',
    'alerting': '告警中'
  }
  return textMap[status] || status
}
</script>

<style scoped>
.detail-section {
  margin-bottom: 24px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 12px;
  border: 1px solid #e4e7ed;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.detail-section:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.detail-section:last-child {
  margin-bottom: 0;
}

.section-title {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid #409eff;
  color: #303133;
  font-size: 16px;
  font-weight: 600;
}

.section-title .el-icon {
  margin-right: 8px;
  font-size: 18px;
  color: #409eff;
  background: rgba(64, 158, 255, 0.1);
  padding: 8px;
  border-radius: 8px;
}

.text-muted {
  color: #909399;
}
</style>

